package org.lakesidecdc.outreach.server;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;

import org.lakesidecdc.outreach.client.NotLoggedInException;
import org.lakesidecdc.outreach.client.Project;
import org.lakesidecdc.outreach.client.ProjectService;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class ProjectServiceImpl extends RemoteServiceServlet implements ProjectService {

	private static final long serialVersionUID = -7476919907231275760L;
	private static final Logger LOG = Logger.getLogger(ProjectServiceImpl.class.getName());
	private static final PersistenceManagerFactory PMF =
		JDOHelper.getPersistenceManagerFactory("transactions-optional");
	
	public Project addProject(String name, String description) throws NotLoggedInException {
		checkLoggedIn();
		PersistenceManager pm = getPersistenceManager();
		try {
			pm.makePersistent(new ProjectData(name, description, getUser()));
			return new Project(name, description, getUser());
		} finally {
			pm.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public void removeProject(String name) throws NotLoggedInException {
		checkLoggedIn();
		PersistenceManager pm = getPersistenceManager();
		try {
			long deleteCount = 0;
			Query q = pm.newQuery(ProjectData.class, "user == u");
			q.declareParameters("com.google.appengine.api.users.User u");
			List<ProjectData> projects = (List<ProjectData>) q.execute(getUser());
			for (ProjectData project: projects) {
				if(name.equals(project.getName())) {
					deleteCount++;
					pm.deletePersistent(project);
				}
			}
			if(deleteCount != 1) {
				LOG.log(Level.WARNING, "removeProject deleted " + deleteCount + " projects");
			}
		} finally {
			pm.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public String[] getProjects() throws NotLoggedInException {
		checkLoggedIn();
		PersistenceManager pm = getPersistenceManager();
		List<ProjectData> projects = new ArrayList<ProjectData>();
		try {
			Query q = pm.newQuery(ProjectData.class, "user == u");
			q.declareParameters("com.google.appengine.api.users.User u");
			q.setOrdering("createDate");
			projects = (ArrayList<ProjectData>) q.execute(getUser());
		} finally {
			pm.close();
		}
		return (String[]) projects.toArray(new String[0]);
	}
	
	private void  checkLoggedIn() throws NotLoggedInException {
		if (getUser() == null) {
			throw new NotLoggedInException("Not logged in.");
		}
	}
	
	private String getUser() {
		UserService userService = UserServiceFactory.getUserService();
		return userService.getCurrentUser().getEmail();
	}
	
	private PersistenceManager getPersistenceManager() {
		return PMF.getPersistenceManager();
	}
}
